package org.docshare.zhuishu;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.docshare.http.DataServer;
import org.docshare.http.YangResponse;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/14.
 */
public class BookFragment extends Fragment implements RefleshListView.IReflashListener {
    private  int bookid;
    RefleshListView list;
    public BookFragment(){
        super();
    }
    public BookFragment(int bookid){
        this.bookid = bookid;
    }
    private static final String TAG = "FavFragment";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_booklist,null,false);
        list = (RefleshListView) v.findViewById(R.id.booklist);

        list.setInterface(this);

        onReflash();
        return v;

    }
    private void initList2(Object data){
        String[] from = {"bookname","captitle"};
        int[] to = {R.id.bookname,R.id.captitle};
        SimpleAdapter adapter = new SimpleAdapter(this.getContext(), (List<? extends Map<String, ?>>) data,R.layout.item_fav,from,to);
        list.setAdapter(adapter);
    }

    @Override
    public void onReflash() {
        DataServer.getIns(this.getContext()).dir(bookid, new YangResponse() {
            @Override
            public void onResult(boolean succ, Object response) {
                if(succ){
                    initList2(response);

                }else{
                    Toast.makeText(BookFragment.this.getContext(),(String)response,Toast.LENGTH_LONG).show();
                }
                list.reflashComplete();

            }
        });

    }
}
